#!/bin/bash
#===============================================================================
# logger-http.sh
#
# Control the logger HTTP server.
#===============================================================================

test -x ./config.sh && . ./config.sh &>/dev/null

# Check that the necessary configuration settings are available.

if [ "${LOGGER_HTTP_HOST}" == "" ] ; then
	echo "Setting \${LOGGER_HTTP_HOST} is not set"
	exit 1
fi

if [ "${LOGGER_HTTP_PORT}" == "" ] ; then
	echo "Setting \${LOGGER_HTTP_PORT} is not set"
	exit 1
fi

#-------------------------------------------------------------------------------
# Explain the command line options to the user.
#-------------------------------------------------------------------------------

function SHOW_OPTIONS() {
	EXIT_CODE=${1} ; shift

	cat <<EOF
Usage: ${0} <command>

Commands:
  start    Start the logger HTTP server
  stop     Stop the logger HTTP server
  info     Show status info about the logger HTTP server
  help     Show this message and exit

EOF
	exit ${EXIT_CODE}
}

#-------------------------------------------------------------------------------
# Check if a particular host is reachable over the network.  If the host is
# reachable, the function has an exit code of 0.  If not, the exit code is 1.
#-------------------------------------------------------------------------------

function CHECK_ALIVE() {
	HOST=${1} ; shift

	ping -t 4 -o -q ${HOST} >/dev/null
}

#-------------------------------------------------------------------------------
# Check if the logger is already running. If the server is running, the
# function has an exit code of 0.  If not, the exit code is 1.
#-------------------------------------------------------------------------------

function CHECK_RUNNING() {
	HOST=${1} ; shift
	PORT=${1} ; shift

	nc -z -w5 ${HOST} ${PORT} &>/dev/null && return 0 || return 1
}

#-------------------------------------------------------------------------------
# Check if the logger is not running.  If the server is not running, the
# function has an exit code of 0.  Otherwise, the exit code is 1.
#-------------------------------------------------------------------------------

function CHECK_NOT_RUNNING() {
	HOST=${1} ; shift
	PORT=${1} ; shift

	nc -z -w5 ${HOST} ${PORT} &>/dev/null && return 1 || return 0
}

#-------------------------------------------------------------------------------
# Start the logger on a specified server.
#-------------------------------------------------------------------------------

function START_LOGGER() {
	MESSAGE=${1} ; shift
	HOST=${1} ; shift
	PORT=${1} ; shift
	TEMP=`mktemp -t START_SERVER`

	echo "==== ${MESSAGE} ===="
	echo "  Host: ${HOST}"
	echo "  Port: ${PORT}"
	CHECK_ALIVE ${HOST} \
	|| { echo "  Could not reach host ${HOST}" ; rm -f ${TEMP} ; return ; } \
	&& CHECK_NOT_RUNNING ${HOST} ${PORT} \
	|| { echo "  Logger is already running on ${HOST}:${PORT}" ; rm -f ${TEMP} ; return ; } \
	&& ssh -n -f ${HOST_USERNAME}@${HOST} "nohup java -jar JPF/build/jpf-distrib.jar --no-output" &>${TEMP} \
	|| { echo "  Encountered an error!" ; echo "  Output:" ; cat ${TEMP} ; rm -f ${TEMP} ; return ; } \
	&& { echo "  Started" ; rm -f ${TEMP} ; }
}

#-------------------------------------------------------------------------------
# Stop the logger on a specified server.
#-------------------------------------------------------------------------------

function STOP_LOGGER() {
	MESSAGE=${1} ; shift
	HOST=${1} ; shift
	PORT=${1} ; shift
	TEMP=`mktemp -t STOP_SERVER`

	echo "==== ${MESSAGE} ===="
	CHECK_ALIVE ${HOST} \
	|| { echo "  Could not reach host ${HOST}" ; rm -f ${TEMP} ; return ; } \
	&& CHECK_RUNNING ${HOST} ${PORT} \
	|| { echo "  Logger is not running on ${HOST}:${PORT}" ; rm -f ${TEMP} ; return ; } \
	&& ssh -n -f ${HOST_USERNAME}@${HOST} "nohup java -jar JPF/build/jpf-distrib.jar --no-output" &>${TEMP} \
	|| { echo "  Encountered an error!" ; echo "  Output:" ; cat ${TEMP} ; rm -f ${TEMP} ; return ; } \
	&& { echo "  Stopped" ; rm -f ${TEMP} ; }
}

#-------------------------------------------------------------------------------
# Display information about the logger.
#-------------------------------------------------------------------------------

function INFO_LOGGER() {
	MESSAGE=${1} ; shift
	HOST=${1} ; shift
	PORT=${1} ; shift

	echo "==== ${MESSAGE} ===="
	echo "  Host: ${HOST}"
	echo "  Port: ${PORT}"
	CHECK_ALIVE ${HOST} \
	|| { echo "  Could not reach host ${HOST}" ; return ; } \
	&& CHECK_RUNNING ${HOST} ${PORT} \
	|| { echo "  Logger is not running on ${HOST}:${PORT}" ; return ; } \
	&& wget --tries=2 --quiet --output-document=- http://${HOST}:${PORT}/info \
	|| { echo "  Could not connect to host ${HOST}" ; return ; }
}

#-------------------------------------------------------------------------------
# Process the command line options.
#-------------------------------------------------------------------------------

COMMAND=${1} ; shift

# Check that there is some command.

if [ "${COMMAND}" == "" ] ; then
	echo "No command given"
	SHOW_OPTIONS 1
fi

# Check if there are any extra options.

if [ "${*}" != "" ] ; then
	echo "Unrecognized option \"${1}\""
	SHOW_OPTIONS 1
fi

#-------------------------------------------------------------------------------
# Execute the given command.
#-------------------------------------------------------------------------------


case ${COMMAND} in
	help ) SHOW_OPTIONS 0 ;;
	start) START_LOGGER "logger-http" ${LOGGER_HTTP_HOST} ${LOGGER_HTTP_PORT} ;;
	stop ) STOP_LOGGER "logger-http" ${LOGGER_HTTP_HOST} ${LOGGER_HTTP_PORT} ;;
	info ) INFO_LOGGER "logger-http" ${LOGGER_HTTP_HOST} ${LOGGER_HTTP_PORT} ;;
	*    ) echo "Unknown command \"${COMMAND}\"" ; SHOW_OPTIONS 1 ;;
esac

#===============================================================================
# End of logger-http.sh
#===============================================================================
